<template>
  <a-modal
    title="重新设定密码"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
    style="top: 20px"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="用户账号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入用户账号"
            v-decorator="['username', {}]"
            :readOnly="true"
          />
        </a-form-item>

        <a-form-item
          label="登录密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          hasFeedback
        >
          <a-input
            type="password"
            placeholder="请输入新密码"
            v-decorator="['password', validatorRules.password]"
            :maxLength="20"
            oninput="value=value.replace(/[, ]/g,'')"
            onkeyup="value=value.replace(/[, ]/g,'') "
          />
        </a-form-item>

        <a-form-item
          label="确认密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          hasFeedback
        >
          <a-input
            type="password"
            @blur="handleConfirmBlur"
            placeholder="请再次输入新密码"
            v-decorator="['confirmPassword', validatorRules.confirmpassword]"
            :maxLength="20"
            oninput="value=value.replace(/[, ]/g,'')"
            onkeyup="value=value.replace(/[, ]/g,'') "
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { changePassword } from "@/api/api";

export default {
  name: "PasswordModal",
  data() {
    return {
      visible: false,
      confirmLoading: false,
      confirmDirty: false,
      validatorRules: {
        password: {
          rules: [
            {
              required: true,
              pattern: /^.{6,20}$/,
              message: "登录密码长度必须介于6位和20位之间!",
            },
            {
              validator: this.validateToNextPassword,
            },
          ],
        },
        confirmpassword: {
          rules: [
            {
              required: true,
              // pattern: /^.{6,20}$/,
              message: "请再次输入新密码!",
            },
            {
              validator: this.compareToFirstPassword,
            },
          ],
        },
      },
      userId: "",
      model: {},

      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      form: this.$form.createForm(this),
    };
  },
  created() {
    console.log("created");
  },

  methods: {
    show(userId, loginName) {
      this.form.resetFields();
      this.visible = true;
      this.userId = userId;
      this.$nextTick(() => {
        this.form.setFieldsValue({ username: loginName });
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
      this.disableSubmit = false;
      this.selectedRole = [];
    },
    handleSubmit() {
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true;
          let formData = Object.assign({ userId: this.userId }, values);
          console.log(formData);
          changePassword(formData)
            .then((res) => {
              if (res.code == "00000") {
                this.$message.success("修改成功");
                this.$emit("ok");
              } else {
                this.$message.warning("修改失败");
              }
            })
            .finally(() => {
              this.confirmLoading = false;
              this.close();
            });
        }
      });
    },
    handleCancel() {
      this.close();
    },
    validateToNextPassword(rule, value, callback) {
      const form = this.form;
      const confirmpassword = form.getFieldValue("confirmpassword");
      console.log("confirmpassword==>", confirmpassword);
      if (value && confirmpassword && value !== confirmpassword) {
        callback("两次输入的密码不一样！");
      }
      if (value && this.confirmDirty) {
        form.validateFields(["confirm"], { force: true });
      }
      callback();
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form;
      if (value && value !== form.getFieldValue("password")) {
        callback("两次输入的密码不一样！");
      } else {
        callback();
      }
    },
    handleConfirmBlur(e) {
      const value = e.target.value;
      this.confirmDirty = this.confirmDirty || !!value;
    },
  },
};
</script>